home *** CD-ROM | disk | FTP | other *** search
| Text File | 1989-05-02 | 9.6 KB | 285 lines | [TEXT/MPS ] |
- ! FILE: Sample.sim
- ! This little program is written to demonstrate the Simula interface to the
- ! Macintosh Toolbox. It displays a single, fixed size window in which the user
- ! can enter and edit text. It is written to correspond to similar examples
- ! written in pascal and other languages.
- !
- ! Many routine tasks are automatically taken care of in the SIMULA toolbox
- ! interface:
- ! - Standard events are filtered out (for menu selection, system events etc) and
- ! the events targeted for application Windows are dispatched to the appropriate
- ! Window process. The Window is selected and the coordinates are converted to
- ! the local coordinates of the Window.
- ! - Menu selection is done automatically so the user program only has to register
- ! a menu item to take care of the application specific actions when selected
- ! by the user. There is also a ready made menu for the leftmost Apple menu
- ! which runs desk accessoirs etc.
- !
- ! This file is "compiled" with the command:
- !
- ! Rez sample.r -o sample -append
- ! Setfile -a B sample -c LUND -t APPL
- ! simcomp sample
- ! the program should be linked with:
- ! simld sample -toolbox -APPL
- ! which means that it can be executed as an application.
- ! This sequence can be generated by entering "make sample"
- ! --------------------------------------------------------------------------- ;
- begin
-
- external class MacEvent="::SInterfaces:MacEvent";
- external class MacProcessMgr="::SInterfaces:MacProcessMgr";
- external class MacPoint="::SInterfaces:MacPoint";
- external class MacRect="::SInterfaces:MacRect";
- external class MacWindow="::SInterfaces:MacWindow";
- external class MacTextEdit="::SInterfaces:MacTextEdit";
-
- external class DefaultMenuProcess="::SInterfaces:DefaultMenuProcess";
- external class Macmenubar="::SInterfaces:MacMenuBar";
- external class MacMenu="::SInterfaces:MacMenu";
- external class Macmenuitem="::SInterfaces:MacMenuitem";
- external class DefaultAppleMenu="::SInterfaces:DefaultAppleMenu";
-
- external class MacDialogMgr="::SInterfaces:MacdialogMgr";
- external class MacDialog="::SInterfaces:Macdialog";
- external class MacCursor="::SInterfaces:MacCursor";
-
- !---------------------------------------------------
- ! Global data structures and singular system objects
- !---------------------------------------------------;
-
- ref(MacProcessMgr) theMGR; ! This class takes care of events and ;
- ! process scheduling - always make ONE ;
- ref(MacMenuBar) theMenuBar; ! -- Create also one MenuBar and one -- ;
- ref(defaultMenuProcess) theMenuBarProcess; ! -- Process to handle it -- ;
-
- ! In this exsample there is only one Window and associated Process ;
- ref(TextWindow) myTextWindow;
- ref(TextWindowProcess) myTextWindowProcess;
-
- ! ID:s of Menu descriptions in the resource file. ;
- short integer fileID=129, editID=130;
- ref(DefaultAppleMenu) theAppleMenu;
- ref(MacMenu) theFileMenu, theEditMenu;
-
- !---------------------------------------------------
- ! Window and Process definition
- !---------------------------------------------------;
-
- ! TextWindow: in this example all window output is controlled
- ! by the TE-package, so the TextWindow is almost Empty
- !-------------------------;
- MacWindow class TextWindow;
- begin
- ref(MacTextEdit) myTE;
- short integer windowId=128; ! The description of this Window is stored ;
- GetNewWindow(windowId, none);! in the resourcefile: sample.r, ID: 128 ;
- end;
-
- ! TextWindowProcess: Waits for events targeted for the Window and
- ! does the neccessary actions as they arrive.
- !--------------------------------- ;
- MacProcess class TextWindowProcess(MyW); ref(TextWindow) MyW;
- begin
- ref(MacCursor) iBeamCursor, arrowCursor;
- ref(MacRect) TXRect,WPort;
- ref(MacEvent) theEvent;
- ref(MacPoint) ClickPoint;
-
- boolean procedure PtInRect(aPoint, aRect);
- ref(MacPoint) aPoint; ref(MacRect) aRect;
- begin
- inspect aPoint do
- inspect aRect do
- PtInRect:= Top<V and then V<Bottom and then
- Left<H and then H<Right;
- end;
- ! -- Create Objects and initialize data structures
- ! -- local to this process -- ;
-
- TXRect:-new MacRect;
- MyW.GetPortRect(TXRect); ! Get the Bounds of the Window ;
- MyW.myTE:-new MacTextEdit;
- MyW.myTE.TEnew(TXRect,TXRect);
-
- theEvent:-new MacEvent;
- ClickPoint:-new MacPoint;
- WPort:-new MacRect;
- MyW.GetPortRect(WPort);
-
- iBeamCursor:-new MacCursor;
- iBeamCursor.GetCursor(TConst.iBeamCursor);
- arrowCursor:-new MacCursor;
-
- ! -- Enable the Events we are prepared to take care of.
- ! -- Other events for this process are discarded. --;
- EnableEvent(TConst.keyDown);
- EnableEvent(TConst.mouseDown);
- EnableEvent(TConst.updateEvt);
- EnableEvent(TConst.activateEvt);
- EnableEvent(TConst.NullEvent);
-
- myW.myTE.TEActivate; ! Our window is active to start with ;
- ! -- Main event loop of this process -- ;
- while true do
- begin
- ! -- Set cursor to match being inside/outside the Window --;
- if MyW == theMGR.FrontWindow then
- begin
- theMGR.EventMgr.GetMouse(ClickPoint);
- if PtInRect(ClickPoint, WPort) then
- iBeamCursor.SetCursor
- else
- arrowCursor.InitCursor;
- end;
- ! -- This process Halts here until an Event is available,
- ! -- but since we have enables also NullEvents we will get
- ! -- these repeatedly when our window is active. --;
- WaitNextEvent(theEvent);
- ! -- Analyze What kind of Event we gott -- ;
- if theEvent.what=tconst.NullEvent then
- myW.myTE.TEIdle !call TextEdit to make vertical bar blink;
- else if theEvent.what=TConst.keyDown then
- myW.myTE.TEKey(theEvent.keyPressed)
- else if theEvent.what=TConst.mouseDown then
- begin
- theEvent.CopyPoint(Clickpoint);
- myW.myTE.TEClick(ClickPoint,theEvent.Shiftkey);
- end
- else if theEvent.what=TConst.updateEvt then
- begin
- myW.BeginUpdate;
- myW.EraseRect(TXRect);
- myW.myTE.TEUpdate(TXRect);
- myW.EndUpdate;
- end
- else if theEvent.what=TConst.activateEvt then
- begin
- if theEvent.activated then
- myW.myTE.TEActivate
- else
- myW.myTE.TEDeactivate;
- end;
- end;
- end --- Text Window process ---;
-
- ! ---------------------------------------------------
- ! Menu related classes and procedures
- ! ---------------------------------------------------;
-
- ! InitMenus: This procedure initializes the menus and creates
- ! the neccessary MenuItem sub-class objects to react to
- ! menu selection. In this case there are three pull-down menus.
- !------------------;
- procedure InitMenus;
- begin
- ! -- Apple-menu: use the default version but register
- ! -- an About-item of our own. ;
- theAppleMenu:-new DefaultAppleMenu;
- theMenuBar.InsertMenu(theAppleMenu,0);
- theAppleMenu.RegisterItem(new AboutSimItem,1);
- ! -- the File menu we are building directly --;
- theFileMenu:-new MacMenu;
- theFileMenu.NewMenu(FileId,"File");
- theMenuBar.InsertMenu(theFileMenu,0);
- theFileMenu.AppendMenu("Quit");
- theFileMenu.RegisterItem(new QuitItem,1);
- ! -- The Edit menu is described in the resource file
- ! -- so it is simply read in. For each item there
- ! -- is created an object to do the actions. They
- ! -- all have a procedure "doMenu" that is called
- ! -- when the alternative is selected. -- ;
- theEditMenu:-new MacMenu;
- theEditMenu.getMenu(EditId);
- theMenuBar.InsertMenu(theEditMenu,0);
- theEditMenu.RegisterItem(new CutItem,3);
- theEditMenu.RegisterItem(new CopyItem,4);
- theEditMenu.RegisterItem(new PasteItem,5);
- theEditMenu.RegisterItem(new ClearItem,6);
- ! -- Draw the updated menu Bar to make it visible -- ;
- theMenuBar.DrawMenuBar;
- end -- Init menus -- ;
-
- ! MacMenuItem: One subclass for each menu alternative.
- !-------------------------;
- MacMenuItem class QuitItem;
- begin
- procedure doMenu(m,i); integer m,i;
- theMGR.Stop;
- end;
-
- MacMenuItem class CutItem;
- begin
- procedure doMenu(m,i); integer m,i;
- myTextWindow.myTE.TECut;
- end;
-
- MacMenuItem class CopyItem;
- begin
- procedure doMenu(m,i); integer m,i;
- myTextWindow.myTE.TECopy;
- end;
-
- MacMenuItem class PasteItem;
- begin
- procedure doMenu(m,i); integer m,i;
- myTextWindow.myTE.TEPaste;
- end;
-
- MacMenuItem class ClearItem;
- begin
- procedure doMenu(m,i); integer m,i;
- myTextWindow.myTE.TEDelete;
- end;
-
- MacMenuItem class AboutSimItem;
- begin
-
- procedure doMenu(m,i); integer m,i;
- begin
- ref(MacRect) itemRect;
- ref(MacDialogMgr) myDialogMgr;
- ref(MacDialog) myDialog;
- short integer itemHit,itemType,
- aboutMeDLOG = 128, okButton = 1, authorItem = 2,languageItem = 3;
- integer ItemHdl;
-
- itemRect:-new MacRect;
-
- myDialogMgr:- new MacDialogMgr(theMGR);
- myDialog:-new MacDialog;
- myDialog.GetNewDialog(aboutMeDLOG, none);
-
- myDialog.GetDitem( authorItem, itemType, itemHdl, itemRect);
- mydialog.GetDitem(languageItem, itemType, itemHdl, itemRect);
-
- itemhit:=0;
- while itemHit <> okButton do
- myDialogMgr.ModalDialog(0, itemHit);
-
- myDialog.disposdialog;
- myTextWindow.myTE.TEActivate;
- end;
- end;
-
- !-------------------------------------------------
- ! Initialize Global objects and data structures
- !-------------------------------------------------;
- ! -- One of each of these -- ;
- theMGR:-new MacProcessMgr;
- theMenuBar:-new MacMenuBar;
- theMenuBarProcess:- new DefaultMenuProcess(theMenuBar);
- theMGR.RegisterMenuProcess(theMenuBarProcess);
- InitMenus; ! -- Create menu alternatives -- ;
-
- ! -- Create our single window and make it known to the Manager -- ;
- myTextWindow:-new TextWindow;
- theMGR.RegisterWindow(myTextWindow);
- ! -- Create the process to control the window and make it known --;
- myTextWindowProcess:-new TextWindowProcess(myTextWindow);
- theMGR.RegisterProcess(myTextWindowProcess, myTextWindow);
-
- ! -- Leave control to the manager --;
- theMGR.Run;
- ! -- code here will be executed when the manager has been "Stopped";
- end